package com.sctu.EMSApp;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.FamousSpotOneInfo;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/9.
 */
public class FamousOneInfoActivity extends Activity implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private TextView tourSightInfo;
    private TextView title;
    private List<View> listviews;
    private List<ImageView> listimageView = new ArrayList<ImageView>();
    private ImageView imageView;
    private ImageView returnfragment;


    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sight_details);
        initview();
    }

    private void initview() {

        //对返回按钮监控，进行返回
        returnfragment = (ImageView) findViewById(R.id.sight_details_return_img);
        returnfragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FamousOneInfoActivity.this.finish();
            }
        });

        int id = getIntent().getIntExtra("id", 0);
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL_FAMOUS_SPOT_ONE + id, listener, errorlistener);

    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {
            Log.e("Json", jsonstring);

            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            final FamousSpotOneInfo results = JSON.parseObject(jsonstring, FamousSpotOneInfo.class);


            tourSightInfo = (TextView) findViewById(R.id.famous_spot_one_text);
            title = (TextView) findViewById(R.id.sight_details_title_txt);
            title.setText(results.getResults().get(0).getTitle());
            //对每个景点的文字信息进行设置
            viewPager = (ViewPager) findViewById(R.id.famous_spot_info_viewpager);
            viewPager.setOnPageChangeListener(FamousOneInfoActivity.this);
            tourSightInfo.setText(results.getResults().get(0).getContent());
            listviews = new ArrayList<View>();
            for (int i = 0; i < results.getResults().get(0).getFileList().size(); i++) {

                view = (View) LayoutInflater.from(FamousOneInfoActivity.this).inflate(R.layout.item_viewpager_sight_details,null);
                imageView = (ImageView) view.findViewById(R.id.item_viewpager_sight_details_image);
                //获取网络图片并加载到相应的view上面
                GetVolleyHttp.getInstance(FamousOneInfoActivity.this).
                        setImageLoader(imageView, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + results.getResults().get(0).getFileList().get(i).getPath());
                //添加到image集合中
                listviews.add(view);

            }

            viewPager.setAdapter(new ViewPagerAdapter(listviews));

            /**
             * 动态的对小圆点进行控制
             */
            LinearLayout mainLayout = (LinearLayout) findViewById(R.id.famous_spot_one_info_picture_relative);
            if (results.getResults().get(0).getFileList().size() != 1&&results.getResults().get(0).getFileList().size() !=0) {

                for (int i = 0; i < results.getResults().get(0).getFileList().size(); i++) {
                    LinearLayout linearLayout = new LinearLayout(getApplicationContext());
                    LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                    //设置每个小圆点距离左边的间距

                    margin.setMargins(10, 0, 0, 0);
                    imageView = new ImageView(FamousOneInfoActivity.this);

                    //设置每个小圆点的宽高
                    imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
                    listimageView.add(imageView);
                    if (i == 0) {
                        // 默认选中第一张图片
                        listimageView.get(i)
                                .setBackgroundResource(R.drawable.scenic_tour_round_after_click_click);
                    } else {
                        //其他图片都设置未选中状态
                        listimageView.get(i)
                                .setBackgroundResource(R.drawable.scenic_tour_round_after_click_unclick);
                    }
                    linearLayout.addView(listimageView.get(i), margin);
                    mainLayout.addView(linearLayout);
                }
            }
        }

    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(FamousOneInfoActivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();

        }
    };
    
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
//遍历让当前选中图片下的小圆点设置颜色
        for (int i = 0; i < listimageView.size(); i++) {
            listimageView.get(i)
                    .setBackgroundResource(R.drawable.scenic_tour_round_after_click_click);

            if (position != i) {
                listimageView.get(i)
                        .setBackgroundResource(R.drawable.scenic_tour_round_after_click_unclick);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
